package com.zjitc.backend_shop.controller.Admin;

import com.github.pagehelper.PageInfo;
import com.zjitc.backend_shop.pojo.Category;
import com.zjitc.backend_shop.pojo.Goods;
import com.zjitc.backend_shop.pojo.GoodsAlbum;
import com.zjitc.backend_shop.service.GoodsAlbumService;
import com.zjitc.backend_shop.service.GoodsService;
import com.zjitc.backend_shop.utils.APIResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王新瑜
 * @date 2024/12/20 10:07
 */

@Controller
@RestController
@RequestMapping("/admin/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsAlbumService goodsAlbumService;

    //查询单个商品
    @GetMapping
    public APIResponse<Goods> findGoodById(@RequestParam(value = "id", required = false) Integer id,
                                           HttpServletRequest request) {
        Goods good = goodsService.findGoodsById(id);
        List<GoodsAlbum> albums = goodsAlbumService.findGoodsAlbumByGoodsId(good.getId());
        for (GoodsAlbum album : albums) {
            if (album.getPicture() != null) {
                album.setPicture(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + album.getPicture());
            }
        }
        System.out.println(albums);
        good.setAlbum(albums);

        return new APIResponse<>(0, "", good);
    }
    @PostMapping("/add")
    public APIResponse addGoods(@RequestBody Goods goods) {
        APIResponse apiResponse = new APIResponse<>();
        Goods goods1 = goodsService.addGoods(goods);
        if (goods1!=null) {
            apiResponse.APIResponseOK(0, "添加成功", null);
        } else {
            apiResponse.APIResponseError(1000, "添加失败", null);
        }
        if(goods.getAlbum()!=null){
            for (GoodsAlbum s : goods.getAlbum()) {
                GoodsAlbum goodsAlbum = new GoodsAlbum();
                goodsAlbum.setGoods_id(goods1.getId());
                goodsAlbum.setPicture(s.getPicture());
                goodsAlbumService.addGoodsAlbum(goodsAlbum);
            }
        }
        return apiResponse;
    }

    @PostMapping("/save")
    public APIResponse saveGoods(@RequestBody Goods goods) {
        APIResponse apiResponse = new APIResponse<>();
        if (goodsService.updateGoods(goods)) {
            apiResponse.APIResponseOK(0, "修改成功", null);
        } else {
            apiResponse.APIResponseError(1000, "修改失败", null);
        }
        return apiResponse;
    }

    @PostMapping("/del")
    public APIResponse deleteGoods(@RequestBody Goods goods) {
        APIResponse apiResponse = new APIResponse<>();

        if (goods.getId() == null) {
            apiResponse.APIResponseError(1001, "ID不能为空", null);
        } else {

            if (goodsService.deleteGoods(goods.getId()) && goodsAlbumService.deleteGoodsAlbum(goods.getId())) {
                apiResponse.APIResponseOK(0, "删除成功", null);
            } else {
                apiResponse.APIResponseError(1000, "删除失败", null);
            }
        }
        return apiResponse;
    }
//    @GetMapping("/detail")
//    public APIResponse<Goods> findById(@RequestParam(value = "id", required = false) Integer id) {
//        Goods good = goodsService.findGoodsById(id);
//        APIResponse<Goods> apiResponse = new APIResponse<>(0, "", good);
//        return apiResponse;
//    }
//    @GetMapping("/album")
//    public APIResponse<GoodsAlbum> find(@RequestParam(value = "id", required = false) Integer id) {
//        GoodsAlbum good = GoodsService.findGoodsById(id);
//        APIResponse<GoodsAlbum> apiResponse = new APIResponse<>(0, "", good);
//        return apiResponse;
//    }

    //分页查询
    @GetMapping("/list")
    public APIResponse<Map<String, Object>> getGoodsByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                           @RequestParam(value = "pagesize", defaultValue = "2") Integer pagesize,
                                                           HttpServletRequest request) {
        PageInfo<Goods> pageInfo = goodsService.getGoodsByPage(page, pagesize);
        List<Goods> goods = pageInfo.getList();
        for (Goods good : goods) {
            if (!good.getPicture().equals("")) {
                good.setPicture(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + good.getPicture());
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("list", goods);
        result.put("total", pageInfo.getTotal());
        return new APIResponse<>(0, "", result);
    }
}
